﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;

namespace CNCSoft.ErpApps.TMS.longACSS
{
    public partial class frmBookPhongDialog : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        string _maphong, _malop;
        bool _datphong = false;
        bool _traphong = false;

        public frmBookPhongDialog()
        {
            InitializeComponent();
        }

        public frmBookPhongDialog(string maphong, string malop, bool datphong, bool traphong)
        {
            _maphong = maphong;
            _malop = malop;
            _datphong = datphong;
            _traphong = traphong;
            InitializeComponent();
        }

        private void frmBookPhongDialog_Load(object sender, EventArgs e)
        {
            LoadControl();
        }

        private void btn_DatPhong_Click(object sender, EventArgs e)
        {
            try
            {
                DatPhong(_maphong, _malop, Convert.ToDateTime(dateEdit_NgayDatPhong.EditValue), memoEdit_NoiDung.EditValue.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_TraPhong_Click(object sender, EventArgs e)
        {
            try
            {
                TraPhong(_maphong, _malop);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region "Coding"
        private void LoadControl()
        {
            LoadData();
            if (_datphong)
            {
                btn_DatPhong.Visible = true;
                btn_TraPhong.Visible = false;
            }
            else if (_traphong)
            {
                btn_DatPhong.Visible = false;
                btn_TraPhong.Visible = true;
            }
        }

        private void LoadData()
        {
            try
            {
                lbl_Phong.Text = _maphong;
                lbl_MaKhoaHoc.Text = _malop;
                dateEdit_NgayDatPhong.EditValue = DateTime.Now.ToShortDateString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DatPhong(string maphong, string malop, DateTime ngayxepphong, string noidung)
        {
            try
            {
                string sql = @"insert into LOP_PHONG(MaPhong, MaLop, NgayXepPhong, NoiDung) values (@maphong, @malop, @ngayxep, @noidung)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@maphong", maphong);
                cmd.Parameters.AddWithValue("@malop", malop);
                cmd.Parameters.AddWithValue("@ngayxep", ngayxepphong);
                cmd.Parameters.AddWithValue("@noidung", noidung);
                int i = 0;
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    lbl_KetQua.Text = "Đã đặt phòng " +lbl_Phong.Text+ " thành công";
                    UpdateStatusRoom();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void TraPhong(string maphong, string malop)
        {
            try
            {
                string sql = @"delete LOP_PHONG where MaPhong = '" + maphong + "' and MaLop = '" + malop + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                int i = 0;
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    lbl_KetQua.Text = "Đã trả phòng " + lbl_Phong.Text + " thành công";
                    UpdateStatusRoom();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UpdateStatusRoom()
        {
            try
            {
                string status = "Operating";
                if (_datphong)
                    status = "Operating";
                else if (_traphong == true && KiemTraPhongConSuDungKhong(_maphong) == false)
                    status = "Available";
                string sql = @"update DM_PHONG set [Status] = '"+status+"' where MaPhong = '"+_maphong+"'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                int i = 0;
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private bool KiemTraPhongConSuDungKhong(string maphong)
        {
            try
            {
                int i = 0;
                string sql = @"select COUNT(*) from LOP_PHONG where MaPhong = '"+maphong+"'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
                    i = Convert.ToInt32(cmd.ExecuteScalar());
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        #endregion
    }
}
